local filename = "bankpwdset.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog =require "filelog"
local md5 = require "md5"
local queue = require "skynet.queue"
local base = require "base"

local redisdao = require "redisdao"
local json = require "cjson.safe"
json.encode_sparse_array(true, 1, 1)

require "pubenum"

local service = nil
local CMD = {
	service = nil
}

--是否已从redis读取
local is_get_from_redis = false

local cs = queue()

--
--手机认证消息处理
--必要参数：verification_code 验证码
--可选参数：isreset 是否是重置密码
--
local function bankpwdset(rid, msgbody)
	if not service then
		service = CMD.service
	end

    --验证码为空，尝试从redis读取一次
    local key = rid or service.rid or 0
    if not is_get_from_redis and not service.cur_verif_code then
        local status,data = redisdao.query_data(".gmmqredis", "get", key .. ":cur_verifinfo")
        local cur_verifinfo = json.decode(data) or { }
        if cur_verifinfo then
            service.cur_verif_phonenum = tostring(cur_verifinfo.cur_verif_phonenum)
            service.cur_verif_code = tostring(cur_verifinfo.cur_verif_code)
            service.cur_verif_code_expiratetime = tonumber(cur_verifinfo.cur_verif_code_expiratetime)
        end
        is_get_from_redis = true
    end

    --  --判断是否设置过
    -- if service.money.lock_password and service.money.lock_password ~= "" then
    --     return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "已经设置过密码了"}
    -- end

    if not service.cur_verif_phonenum or not service.cur_verif_code 
    or service.cur_verif_phonenum ~= msgbody.phonenum then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "错误的验证码"}
    end

    local verif_code = msgbody.verification_code
    local cur_time = timetool.get_time()

    if verif_code == "" or verif_code ~= service.cur_verif_code then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "错误的验证码"}
    end

    if cur_time > service.cur_verif_code_expiratetime then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "验证码已过期,请重新获取"}
    end

    --检测用户名，密码
    local bankpwd = msgbody.bankpwd or ""
    local len = string.len(bankpwd) or 0
    if len < 6 or len > 32 then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "无效的密码"}
    end

    --更新rs_user表
    -- local updatelist = {lock_password = bankpwd}
    local updatelist = {lock_password = md5.sumhexa(bankpwd .. key)}
    local result = cluster.call("datanode", ".router", "update_rk", rid, "rs_money", nil, updatelist)
    if not result or result[1] == 0 then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "密码已存在"}
    end

    service.cur_verif_phonenum = nil
    service.cur_verif_code = nil
    service.cur_verif_code_expiratetime = nil

    --删除redis记录
    redisdao.query_data(".gmmqredis", "del", key .. ":cur_verifinfo")
    
    --更新数据
    service.money.lock_password = updatelist.lock_password
	
	return {bankpwd = bankpwd}
end

--处理玩家绑定手机
function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end
    return cs(bankpwdset, rid, msgbody)
end

return CMD